package org.agilelang.db.toagile;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.util.Inflector;

public class DlgSelectScaffold extends javax.swing.JDialog {

    List<Model> models;
    List<Controller> controllers = new ArrayList<Controller>();
    boolean status = false;
    DefaultTableModel tbm = new DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Name Model(singularize)", "Name Controller", "Description", "Create"
            }) {
        Class[] types = new Class[]{
            java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
        };

        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            if (column == 0 || column == 1) {
                return false;
            } else {
                return true;
            }
        }
    };

    public DlgSelectScaffold(java.awt.Frame parent, boolean modal, List<Model> models, List<Controller> controller_scaffold) {
        super(parent, modal);
        initComponents();
        this.models = models;
        this.controllers = controller_scaffold;
        tblTable.setModel(tbm);
        int t = models.size();
        int f = 0;
        for (int x = 0; x < t; x++) {
            Model model = models.get(x);
            if (model.options.equals("true")) {
                f = 0;
                String controller_name = Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(model.name));
                int tt = controllers.size();
                for (int xx = 0; xx < tt; xx++) {
                    Controller controller = controllers.get(xx);
                    if (controller_name.equals(controller.name)) {
                        Object[] obj = {controller.model_scaffold, controller.name, controller.description, true};
                        tbm.addRow(obj);
                        f = 1;
                    }
                }
                if (f == 0) {
                    Object[] obj = {model.name, controller_name, "", false};
                    tbm.addRow(obj);
                }
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTable = new javax.swing.JTable();
        btnGenerateScaffold = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Models"));

        jScrollPane1.setViewportView(tblTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnGenerateScaffold.setText("Generate Scaffold");
        btnGenerateScaffold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateScaffoldActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGenerateScaffold)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGenerateScaffold)
                    .addComponent(btnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenerateScaffoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateScaffoldActionPerformed
        int t = tbm.getRowCount();
        for (int x = 0; x < t; x++) {
            if (tbm.getValueAt(x, 3).toString().equals("true")) {
                Controller co = new Controller();
                co.name = tbm.getValueAt(x, 1).toString();
                co.type = "scaffold";
                co.model_scaffold = tbm.getValueAt(x, 0).toString();
                controllers.add(co);
                System.out.println(tbm.getValueAt(x, 0));
            }
        }
        status = true;
        setVisible(false);

    }//GEN-LAST:event_btnGenerateScaffoldActionPerformed

    public boolean isStatus() {
        return status;
    }
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed
    public List<Controller> getControllers() {
        return controllers;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnGenerateScaffold;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTable;
    // End of variables declaration//GEN-END:variables
}
